package com.unc.allergyex.helpers;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;

import com.unc.allergyex.AllergyEX;
import com.unc.allergyex.Application;
import com.unc.allergyex.objects.AllergyPreference;
import com.unc.allergyex.objects.db.*;

/**
 * This class is intended to abstract out all knowledge
 * of how database objects are persisted.  This class
 * provides interfaces to store and load database 
 * objects.  
 * @author Phillip and Stephanie
 */
public class ObjManager {
	/* PURPOSE:  Abstract out all knowledge of how objects are persisted.
	 * (When you need an object, just ask "Object Man"!) 
	 * 
	 * USE 1:  Put your objects into memory from DB
	 * USE 2:  Get the restaurant object.  The Restaurant object can help you from there.
	 */

	// DB Object PUT methods...  Just call these to stow an object!
	public static void setRestaurant(Restaurant theRestaurant){
		Application.restaurant = theRestaurant;
		return;
	}
	public static void putCategory(Category theCategory){
		Application.catMap.put(theCategory.getId(), theCategory);
		return;
	}
	public static void putDish(Dish theDish){
		Application.dishMap.put(theDish.getId(), theDish);
		return;
	}
	public static void putComponent(Component theComponent){
		Application.compMap.put(theComponent.getId(), theComponent);
		return;
	}
	public static void putAllergen(Allergen theAllergen){
		Application.allMap.put(theAllergen.getId(), theAllergen);
		return;
	}
	public static void setAllergyPreferenceList(ArrayList<AllergyPreference> ap){
		Application.allergyPreferenceList = ap;
	}
	public static void setAllergenList(Allergen[] al){
		Application.allergenList = al;
	}
	public static void setRestaurantList (Restaurant[] al){
		Application.restaurantList = al;
	}
	public static void setAmbiguousRestaurantList(Restaurant[] list){
		Application.ambiguousRestaurantList = list;
	}
	public static void setMapSearchRestaurantList(Restaurant[] list){
		Application.mapSearchRestaurantList = list;
	}
	
	
	// DB Object FETCH methods...  Call these to retrieve objects
	
	public static Restaurant getRestaurant(){
		//USE THIS METHOD
		return Application.restaurant;
	}
	public static Category getCategory(String id){
		return Application.catMap.get(id);
	}
	public static Dish getDish(String id){
		return Application.dishMap.get(id);
	}
	public static Component getComponent(String id){
		return Application.compMap.get(id);
	}
	public static Allergen getAllergen(String id){
		return Application.allMap.get(id);
	}
	public static Allergen[] getAllergenList(){
		return Application.allergenList;
	}
	public static Restaurant[] getRestaurantList(){
		Restaurant[] list = Application.restaurantList;
		if (list == null){
			list = new Restaurant[0];
		}
		return list;
	}
	public static ArrayList<AllergyPreference> getAllergyPreferenceList(){
		return Application.allergyPreferenceList;
	}
	public static ArrayList<String> getAllergicList(){
		ArrayList<String> allergicList = new ArrayList<String>();
		for(AllergyPreference a : Application.allergyPreferenceList){
			if(a.getAllergic()){
				allergicList.add(a.getAllergen().getId());
			}
		}
		return allergicList;
	}
	public static Restaurant[] getAmbiguousRestaurantList(){
		return Application.ambiguousRestaurantList;
	}
	public static Restaurant[] getMapSearchRestaurantList(){
		Restaurant[] list = Application.mapSearchRestaurantList;
		if (list == null){
			list = new Restaurant[0];
		}
		return list;
	}
	
	
	//DB Object BULK FETCH methods...  IF YOUR NAME IS NOT FILLUP (lololol), DO NOT CALL THESE
	
	public static ArrayList<Category> getCategoryList(String[] ids){
		ArrayList<Category> list = new ArrayList<Category>();
		for(int i = 0; i < ids.length; i++){
			list.add(getCategory(ids[i]));
		}
		return list;
	}
	public static ArrayList<Dish> getDishList(String[] ids){
		ArrayList<Dish> list = new ArrayList<Dish>();
		for(int i = 0; i < ids.length; i++){
			list.add(getDish(ids[i]));
		}
		return list;
	}
	public static ArrayList<Component> getComponentList(String[] ids){
		ArrayList<Component> list = new ArrayList<Component>();
		for(int i = 0; i < ids.length; i++){
			list.add(getComponent(ids[i]));
		}
		return list;
	}
	public static ArrayList<Allergen> getAllergenList(String[] ids){
		ArrayList<Allergen> list = new ArrayList<Allergen>();
		for(int i = 0; i < ids.length; i++){
			list.add(getAllergen(ids[i]));
		}
		return list;
	}
	
	
	// Stephanie's strange method
	public static boolean isFreshInstall(Context poop){
		SharedPreferences storedJsonStrings = poop.getSharedPreferences(AllergyEX.VERSION_NUMBERS_PREFS, 0);
		final String freshFlag = "";
		String result = storedJsonStrings.getString(AllergyEX.ALLERGEN_LIST_KEY, freshFlag);
		if (result.equals(freshFlag)){
			return true;
		}
		return false;
	}
	
}
